'use strict';
import * as Koa from 'koa';
import { router, prefix, log } from '../middleware/router';
import { wechatService } from '../service/wechat';
import { merchantService } from '../service/merchant';
import { merchant_wineService } from '../service/merchant_wine';
import { cityinfoService } from '../service/cityinfo';
import { sms } from '../middleware/sms';
import { response } from '../middleware/result';
import config from '../middleware/config';
import { merchantcashService } from "../service/merchantcash";
@prefix('api')
class TestController {
    // @router('/sendmsg')
    // async sendmsg(ctx: Koa.Context) {
    //     sms.sendmsg('123456',  '18510970762');
    // }
    // @router('/sendmsg2')
    // async sendmsg2(ctx: Koa.Context) {
    //     sms.sendmsg2('王松', '川菜馆', '18510970762','王老吉');
    // }
    @router('/web_version')
    async getWebVersion(ctx: Koa.Context) {
        let v = config.get('webVersion') as string;
        response(ctx, v);
    }
    /**
    * 测试get接口
    * @api {get} /test/log 测试post接口
    * @apiDescription 测试post接口
    * @apiName log
    * @apiSampleRequest /test/log
    * @apiGroup test
    * @apiVersion 1.0.0
    */
    // @router('/log', { method: 'post' })
    // @log
    // async test(ctx: Koa.Context) {
    //     //cityinfoService.save(ctx.request.body)
    //     //response(ctx, 'ok');
    //     let result = await cityinfoService.query(ctx.request.body);
    //    response(ctx, result);
    // }
    /**
    * 获取用户openid
    * @api {post} /test/getOpenidByCode 获取用户openid
    * @apiDescription 获取用户openid
    * @apiName getOpenidByCode
    * @apiParam (参数) {String} code 
    * @apiSampleRequest /test/getOpenidByCode
    * @apiGroup test
    * @apiVersion 1.0.0
    */
    @router('/getOpenidByCode')
    async getOpenidByCode(ctx: Koa.Context) {
        ctx.response.set("Access-Control-Allow-Origin", "*");
        let code = ctx.query.code;
        let res: any = await wechatService.getOpenidByCode(code);
        response(ctx, res);
    }

    /**
     * 短信测试接口
     * @api {post} /test/sendsms 短信测试接口
     * @apiDescription 短信测试接口
     * @apiName sendsms
     * @apiParam (参数) {String} phone  手机号
     * @apiParam (参数) {String} validation  验证码
     * @apiSampleRequest /test/sendsms
     * @apiGroup test
     * @apiVersion 1.0.0
     */
    @router('/sendsms', { method: 'post' })
    async sendsms(ctx: Koa.Context) {
        let phone = ctx.request.body.phone;
        let validation = ctx.request.body.validation;
        let res: any = await sms.sendmsg(validation, phone);
        response(ctx, res);
    }
    
    @router('/test22')
    async test1(ctx: Koa.Context) {
       //调用付款
       var pres= await  merchantcashService.merchantcashpay({
        partner_trade_no:'20200117315342901',
        openid:'oWGgR5dVXxowX-q58gXdLXjifjZM',
        wechatrealname:'王松',
        amount: Number(100)*50,
        desc:'SCYY',
      });
        response(ctx, pres);
    }


    @router('/test3', { method: 'post' })
    async test3(ctx: Koa.Context) {
      let currentwines:any= await merchant_wineService.querybymerchantid({mid:'5d8c5a16aa77a003d0f1bebe'});
      let merchants:any= await merchantService.queryall();
      for (let i of merchants) {
if(i._id!='5d8c5a16aa77a003d0f1bebe'){
          for(let k of currentwines.rows){    
            await  merchant_wineService.create({
                merchantid:i._id,
                capacity:k.capacity,
                Alcohol:k.Alcohol,
                Aroma:k.Aroma,
                type:k.type,
                count:0,
                winename:k.winename,
                stock:k.stock,
                price:k.price,
                img:k.img,
                order:k.order,
                remarks:'',
                status:1
              })
          }
      }}
    }

    // @router('/test_order')
    // async test_order(ctx: Koa.Context) {
    //     //组装xml数据
    //     var formData = "<xml>";
    //     formData+='<xml><appid><![CDATA[wx67d87001fe2776b8]]></appid>';
    //     formData+='<bank_type><![CDATA[CFT]]></bank_type>';
    //     formData+='<cash_fee><![CDATA[1]]></cash_fee>';
    //     formData+='<fee_type><![CDATA[CNY]]></fee_type>';
    //     formData+='<is_subscribe><![CDATA[N]]></is_subscribe>';
    //     formData+='<mch_id><![CDATA[1543353851]]></mch_id>';
    //     formData+='<nonce_str><![CDATA[0.j6tfnt2itpg]]></nonce_str>';
    //     formData+='<openid><![CDATA[oWGgR5aFlJdhBi7qjHxqS-2bktmw]]></openid>';
    //     formData+='<out_trade_no><![CDATA[72758244289909]]></out_trade_no>';
    //     formData+='<result_code><![CDATA[SUCCESS]]></result_code>';
    //     formData+='<return_code><![CDATA[SUCCESS]]></return_code>';
    //     formData+='<sign><![CDATA[56629C71896A48263314F3A21840010F]]></sign>';
    //     formData+='<time_end><![CDATA[20190718141902]]></time_end>';
    //     formData+='<total_fee>1</total_fee>';
    //     formData+='<trade_type><![CDATA[JSAPI]]></trade_type>';
    //     formData+='<transaction_id><![CDATA[4200000378201907188467514853]]></transaction_id>';
    //     formData+='</xml>';
    //     let res: any = await wechatService.order_noticetest(formData);
    //     response(ctx, res);
    // }
}